//
//  STSingleMealVC.m
//  YiYueHairdresser
//
//  Created by 路人甲 on 2018/11/2.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "STSingleMealVC.h"
#import "SWSingleMealView.h"

@interface STSingleMealVC (){
    SWSingleMealView *singleMealView ;
    STServiceInfoModel * serviceModel;
    NSArray * arrServiceType;
}

@end

@implementation STSingleMealVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"单项套餐";
    [self wr_setNavBarTintColor:[UIColor blackColor]];
    
    if(!serviceModel){
        serviceModel = [STServiceInfoModel new];
    }
    serviceModel.categoryId = 0;
    serviceModel.stylistId = [[YLUserInfo sharedInstance].stylistId longValue];
    
    
    [self loadsSingleMealView];
    [self loadServiceType];
    if(self.serviceId){
        [self loadServiceSetInfo];
    }
        
}


//查询服务
-(void)loadServiceSetInfo{
    [STHairService getBarberServerInfoWithServiceId:self.serviceId completion:^(id result, NSError *error) {
        if (error) {
            [ZYHCommonMethod showError:error];
        }
        serviceModel = [STServiceInfoModel modelWithJSON:result];
        singleMealView.serviceModel = serviceModel;
        [singleMealView reloadSignleTableView];
    }];
}

//查询类目
-(void)loadServiceType{
    [STHairService getBarberConditionWithPackageType:1 completion:^(id result, NSError *error) {
        if (error) {
            [ZYHCommonMethod showError:error];
        }
         arrServiceType= [NSArray modelArrayWithClass:[STServiceCategoryModel class] json:result];
        
        singleMealView.arrServiceType = arrServiceType;
        [singleMealView reloadSignleTableView];
    }];
}



- (void)loadsSingleMealView {
    singleMealView = [[SWSingleMealView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    singleMealView.serviceModel = serviceModel;
    WeakSelf
    singleMealView.saveBtnAction = ^(id result) {
        [weakSelf actionSaveSingle:result];
    };
    [self.view addSubview:singleMealView];
    [singleMealView reloadSignleTableView];
}

-(void)actionSaveSingle:(STServiceInfoModel * )dicData{
    NSDictionary * oldDic = [dicData modelToJSONObject];
    NSMutableDictionary * dicParameter = [NSMutableDictionary dictionaryWithCapacity:0];
    NSArray * allKeys = oldDic.allKeys;
    for (NSString * key in allKeys) {
        id value = oldDic[key];
        if([value isKindOfClass:[NSNumber class]]){
            NSNumber * valueNum = value;
            if ([key isEqualToString:@"categoryId"] ||[key isEqualToString:@"duration"] || ![valueNum isEqualToNumber:@0]) {
                [dicParameter setObject:valueNum forKey:key];
            }
        }else{
            [dicParameter setObject:value forKey:key];
        }
    }
    
    [dicParameter setObject:@3 forKey:@"serviceType"];
    [dicParameter setObject:@"" forKey:@"decription"]; 
    [STHairService saveBarberServerWithParameters:dicParameter completion:^(id result, NSError *error) {
        if(error){
            [ZYHCommonMethod showError:error];
        }else{
            [ZYHCommonMethod showSuccess:@"保存成功"];
        }
    }];
    
}



@end
